package com.mc.fil.service.impl;

import java.util.List;
import java.util.UUID;

import com.alibaba.fastjson.JSONObject;
import com.mc.common.constant.Constants;
import com.mc.common.core.domain.model.LoginUser;
import com.mc.common.gisserver.GisService;
import com.mc.common.utils.DateUtils;
import com.mc.common.utils.ServletUtils;
import com.mc.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mc.fil.mapper.FilBaseMapper;
import com.mc.fil.domain.FilBase;
import com.mc.fil.service.IFilBaseService;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * 基地信息Service业务层处理
 *
 * @author mc
 * @date 2021-04-12
 */
@Service
public class FilBaseServiceImpl implements IFilBaseService
{
    @Autowired
    private FilBaseMapper filBaseMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private GisService gisService;

    /**
     * 查询基地信息
     *
     * @param id 基地信息ID
     * @return 基地信息
     */
    @Override
    public FilBase selectFilBaseById(String id)
    {
        return filBaseMapper.selectFilBaseById(id);
    }

    /**
     * 查询基地信息列表
     *
     * @param filBase 基地信息
     * @return 基地信息
     */
    @Override
    public List<FilBase> selectFilBaseList(FilBase filBase)
    {
        return filBaseMapper.selectFilBaseList(filBase);
    }

    /**
     * 新增基地信息
     *
     * @param filBase 基地信息
     * @return 结果
     */
    @Override
    public int insertFilBase(FilBase filBase)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        filBase.setCreateTime(DateUtils.getNowDate());
        filBase.setCreateUserId(loginUser.getUsername());
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        MultiValueMap<String, String> params1= new LinkedMultiValueMap<String, String>();
        params1.add("features", "[{\"attributes\": {\n" +
                "    \"NAME\": \""+filBase.getName()+"\",\n" +
                "    \"ID\": \""+uuid+"\",\n" +
                "    \"COOPERATION\": \""+filBase.getCooperation()+"\",\n" +
                "    \"STATUS\": \"0\"\n" +
                "   },\n" +
                "   \"geometry\": {\n" +
                "    \"x\": "+filBase.getX()+",\n" +
                "    \"y\": "+filBase.getY()+"\n" +
                "   }}]");
        MultiValueMap<String, String> params2= new LinkedMultiValueMap<String, String>();
        params2.add("features", "[{\"attributes\": {\n" +
                "    \"NAME\": \""+filBase.getName()+"\",\n" +
                "    \"ID\": \""+uuid+"\",\n" +
                "    \"COOPERATION\": \""+filBase.getCooperation()+"\",\n" +
                "    \"STATUS\": \"0\"\n" +
                "   },\n" +
                "   \"geometry\": {\n" +
                "    \"rings\": [\n" +
                        filBase.getRings()+
                "   ]\n" +
                "   }}]");
        try{
            String jsonObject1 = gisService.restTemplate(params1, Constants.GIS_SERVER_SEED_FARM_URL0+"addFeatures");
            String jsonObject2 = gisService.restTemplate(params2, Constants.GIS_SERVER_SEED_FARM_URL1+"addFeatures");
            String object1 = JSONObject.parseObject(jsonObject1).getJSONArray("addResults").getJSONObject(0).getString("objectId");
            String object2 = JSONObject.parseObject(jsonObject2).getJSONArray("addResults").getJSONObject(0).getString("objectId");
            if(object1!=null&&object2!=null&&!"".equals(object1)&&!"".equals(object2)){
                filBase.setId(uuid);
                return filBaseMapper.insertFilBase(filBase);
            }
        }catch (Exception e){

        }


        return 0;
    }

    /**
     * 修改基地信息
     *
     * @param filBase 基地信息
     * @return 结果
     */
    @Override
    public int updateFilBase(FilBase filBase)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        filBase.setUpdateUserId(loginUser.getUsername());
        filBase.setUpdateTime(DateUtils.getNowDate());
        MultiValueMap<String, String> queryParams= new LinkedMultiValueMap<String, String>();
        queryParams.add("where","ID = \'"+filBase.getId()+"\'");
        String objectId0= gisService.selectObjectId(queryParams,Constants.GIS_SERVER_SEED_FARM_URL0);
        String objectId1= gisService.selectObjectId(queryParams,Constants.GIS_SERVER_SEED_FARM_URL1);
        MultiValueMap<String, String> params1= new LinkedMultiValueMap<String, String>();

        if(filBase.getX() !=null &&!"".equals(filBase.getX()) &&filBase.getY() !=null &&!"".equals(filBase.getY())){
            params1.add("features", "[{\"attributes\": {\n" +
                    "    \"NAME\": \""+filBase.getName()+"\",\n" +
                    "    \"OBJECTID\": "+objectId0+",\n" +
                    "    \"COOPERATION\": \""+filBase.getCooperation()+"\",\n" +
                    "    \"STATUS\": \"0\"\n" +
                    "   },\n" +
                    "   \"geometry\": {\n" +
                    "    \"x\": "+filBase.getX()+",\n" +
                    "    \"y\": "+filBase.getY()+"\n" +
                    "   }}]");
        }else{
            params1.add("features", "[{\"attributes\": {\n" +
                    "    \"NAME\": \""+filBase.getName()+"\",\n" +
                    "    \"OBJECTID\": "+objectId0+",\n" +
                    "    \"COOPERATION\": \""+filBase.getCooperation()+"\",\n" +
                    "    \"STATUS\": \"0\"\n" +
                    "   }\n" +

                    "   }]");
        }

        MultiValueMap<String, String> params2= new LinkedMultiValueMap<String, String>();
        if(filBase.getRings() !=null &&!"".equals(filBase.getRings())){
            params2.add("features", "[{\"attributes\": {\n" +
                    "    \"NAME\": \""+filBase.getName()+"\",\n" +
                    "    \"OBJECTID\": "+objectId1+",\n" +
                    "    \"COOPERATION\": \""+filBase.getCooperation()+"\",\n" +
                    "    \"STATUS\": \"0\"\n" +
                    "   },\n" +
                    "   \"geometry\": {\n" +
                    "    \"rings\": [\n" +
                    filBase.getRings()+
                    "   ]\n" +
                    "   }}]");
        }else{
            params2.add("features", "[{\"attributes\": {\n" +
                    "    \"NAME\": \""+filBase.getName()+"\",\n" +
                    "    \"OBJECTID\": "+objectId1+",\n" +
                    "    \"COOPERATION\": \""+filBase.getCooperation()+"\",\n" +
                    "    \"STATUS\": \"0\"\n" +
                    "   }\n" +
                    "   }]");
        }

        try{
            String jsonObject1 = gisService.restTemplate(params1, Constants.GIS_SERVER_SEED_FARM_URL0+"updateFeatures");
            String jsonObject2 = gisService.restTemplate(params2, Constants.GIS_SERVER_SEED_FARM_URL1+"updateFeatures");
            String object1 = JSONObject.parseObject(jsonObject1).getJSONArray("updateResults").getJSONObject(0).getString("success");
            String object2 = JSONObject.parseObject(jsonObject2).getJSONArray("updateResults").getJSONObject(0).getString("success");
            if(object1!=null&&object2!=null&&"true".equals(object1)&&"true".equals(object2)){
                return filBaseMapper.updateFilBase(filBase);
            }
        }catch (Exception e){

        }
        return 0;
    }


    /**
     * 删除基地信息信息
     *
     * @param id 基地信息ID
     * @return 结果
     */
    @Override
    public int deleteFilBaseById(String id)
    {
        MultiValueMap<String, String> queryParams= new LinkedMultiValueMap<String, String>();
        queryParams.add("where","ID = \'"+id+"\'");
        try {
            String objectId0 = gisService.selectObjectId(queryParams, Constants.GIS_SERVER_SEED_FARM_URL0 );
            String objectId1 = gisService.selectObjectId(queryParams, Constants.GIS_SERVER_SEED_FARM_URL1 );
            MultiValueMap<String, String> params0 = new LinkedMultiValueMap<String, String>();
            params0.add("objectIds", objectId0);
            MultiValueMap<String, String> params1 = new LinkedMultiValueMap<String, String>();
            params1.add("objectIds", objectId1);
            String jsonObject1 = gisService.restTemplate(params0, Constants.GIS_SERVER_SEED_FARM_URL0 + "deleteFeatures");
            String jsonObject2 = gisService.restTemplate(params1, Constants.GIS_SERVER_SEED_FARM_URL1 + "deleteFeatures");
            String object1 = JSONObject.parseObject(jsonObject1).getJSONArray("deleteResults").getJSONObject(0).getString("success");
            String object2 = JSONObject.parseObject(jsonObject2).getJSONArray("deleteResults").getJSONObject(0).getString("success");
            if(object1!=null&&object2!=null&&"true".equals(object1)&&"true".equals(object2)){
                return filBaseMapper.deleteFilBaseById(id);
            }
        }catch (Exception e){

        }
        return 0;
    }
}